package server.container;

import server.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Host implements Valve, Lifecycle {

    private String name;
    private String appBase;

    private List<Context> contextList;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    public List<Context> getContextList() {
        return contextList;
    }

    public void setContextList(List<Context> contextList) {
        this.contextList = contextList;
    }

    @Override
    public void invoke(Request request, Response response) {
        Context context = request.getContext();
        if (Objects.nonNull(context)){
            context.invoke(request,response);
        }else {
            try {
                response.output(HttpProtocolUtil.getHttpHeader404());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void start() {
        contextList.forEach(Context::start);
    }
}
